package com.dogantekin.allseeingeye.sensor;

import java.util.ArrayList;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

public class SensorFactory {
	private static SensorFactory 				instance;
	private final  LocationManager 				locationManager;
	private        ArrayList<ISensorListener>	listeners;
	
	private SensorFactory(Context context) {
		super();
		listeners 	    = new ArrayList<ISensorListener>();		
		
		//gps system
        locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
        LocationListener gpsListener = new LocationListener() {
			@Override
			public void onLocationChanged(Location location) {
				for (ISensorListener listener : listeners) {
					listener.updateLocation(location);
				}
			}

			@Override
			public void onProviderDisabled(String arg0) {
			}

			@Override
			public void onProviderEnabled(String arg0) {
			}

			@Override
			public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
			}        	
        };
        
		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		criteria.setAltitudeRequired(false);
		criteria.setBearingRequired(false);
		criteria.setCostAllowed(true);
		criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
		
        locationManager.requestLocationUpdates(locationManager.getBestProvider(criteria, true), 100, 1, gpsListener);  
		
        //orientation sensor
        SensorEventListener sensorListener = new SensorEventListener(){
        	@Override
     	    public void onSensorChanged(SensorEvent evt) {        		
    	      float values[] = evt.values;    	      
    	      if(evt.sensor.getType() == Sensor.TYPE_ORIENTATION) {
  				for (ISensorListener listener : listeners) {
  					listener.updateOrientation((int) values[0], (int) values[1], (int) values[2]);
  				}
    	      }
    	   }
        	
           @Override
           public void onAccuracyChanged(Sensor sensor, int accuracy){        		
           }
        };   
        SensorManager sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);       
        sensorManager.registerListener(sensorListener,sensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION),SensorManager.SENSOR_DELAY_FASTEST); 
	}
	
	public static SensorFactory getInstance(Context context) {
		if(instance == null) {
			instance = new SensorFactory(context);
		}
		return instance;
	}
	
	public void addListener(ISensorListener listener) {
		listeners.add(listener);		
	}
	
	public boolean removeListener(ISensorListener listener) {
		return listeners.remove(listener);
	}	
	
	public Location getLastKnownLocation() {
		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		criteria.setAltitudeRequired(false);
		criteria.setBearingRequired(false);
		criteria.setCostAllowed(true);
		criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
		
		Location x = new Location(LocationManager.GPS_PROVIDER);
		x.setLatitude(40.793465);
		x.setLongitude(29.467222);
		return x;
		
//		return locationManager.getLastKnownLocation(locationManager.getBestProvider(criteria, true));
	}	
}